package doantotnghiep.daoimpl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

import doantotnghiep.dao.StaffDao;
import doantotnghiep.model.Staff;
import doantotnghiep.model.WorkPlayment;

public class StaffDaoImpl implements StaffDao{

	@Autowired
	private HibernateTemplate hibernateTemplate;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.hibernateTemplate = new HibernateTemplate(sessionFactory);
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}
	
	@Override
	public void deleteStaff(int id) {
		Staff entity = this.findStaffByID(id);
		if (entity != null){
			entity.setStatus(1);
			this.hibernateTemplate.saveOrUpdate(entity);
		}	
	}

	@Override
	public void saveStaff(Staff staf) {
		this.hibernateTemplate.saveOrUpdate(staf);		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Staff> listStaff() {
		return hibernateTemplate.find("from Staff where id > 1 and status = 0");
	}

	@Override
	public synchronized void addStaff(Staff staff) {
		this.hibernateTemplate.saveOrUpdate(staff);	
	}

	@SuppressWarnings("unchecked")
	@Override
	public Staff findStaffByID(int id) {
		List<Staff> staffs = hibernateTemplate.find("from Staff where id ="+ id+"and status = 0");
		if(staffs!=null){
			return staffs.get(0);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Staff> getStaffByKey(String key) {
		List<Staff> list = hibernateTemplate.find("from Staff where fullName like '%"+key+"%'");
		return list;
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Staff> listEmptyUser() {
		List<Staff> list = hibernateTemplate.find("from Staff where userName IS NULL and status = 0");
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<WorkPlayment> getListPosition() {
		List<WorkPlayment> list = hibernateTemplate.find("from WorkPlayment order by id");
		return list;
	}

	@Override
	public void initAdmin() {
		//List<Staff> list = hibernateTemplate.findByNamedQuery("createAdmin"); 	
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Staff> listImportedValidStaff() {
		return hibernateTemplate.find("from Staff where id > 1 and status = 0 and MaPhongBan = 2");
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Staff> listPurchaseValidStaff() {
		// TODO Auto-generated method stub
		return hibernateTemplate.find("from Staff where id > 1 and status = 0 and  MaPhongBan = 3");
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<WorkPlayment> getAllValidPosition() {
		List<WorkPlayment> list = hibernateTemplate.find("from WorkPlayment where id > 1 order by id");
		return list;
	}

}
